<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <!-- <DeptTree class="w-1/4 xl:w-1/5" @select="handleSelect" /> -->
    <!-- <BasicTable @register="registerTable" class="w-3/4 xl:w-4/5" :searchInfo="searchInfo"> -->
    <div class="acc-list-div">
      <BasicTable @register="registerTable" :searchInfo="searchInfo">
        <template #toolbar>
          <a-button type="primary" @click="handleCreate">新增调查单位</a-button>
        </template>
        <template #districtOne="{ record, column }">
          {{ getDistrictOne(record[column.dataIndex]) }}
        </template>
        <template #districtMany="{ record, column }">
          {{ getDistrictMany(record[column.dataIndex]) }}
        </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑单位',
                onClick: handleEdit.bind(null, record),
              },
              // {
              //   icon: 'clarity:plus-line',
              //   tooltip: '添加管理员',
              //   onClick: handleEditMng.bind(null, record),
              // },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除此单位',
                popConfirm: {
                  title: '是否确认删除',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </BasicTable>
    </div>

    <AccountModal
      @register="registerModal"
      @success="handleSuccess"
      roleType="CityUnitAdmin"
      width="calc(50vw)"
      :bodyStyle="{ height: 'calc(50vh)' }"
    />
    <MngModal
      @register="registerModal3"
      @success="handleSuccess"
      width="calc(50vw)"
      :bodyStyle="{ height: 'calc(50vh)' }"
    />
    <AccountPwdModal @register="registerModal2" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, reactive, ref } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getSurveyUnitList, delSurveyUnit } from '/@/api/demo/system';
  import { PageWrapper } from '/@/components/Page';
  import DeptTree from './DeptTree.vue';

  import { useModal } from '/@/components/Modal';
  import AccountModal from './AccountModal.vue';
  import MngModal from './MngModal.vue';
  import AccountPwdModal from './AccountPwdModal.vue';

  import { columns, searchFormSchema } from './account.data';
  import { useGo } from '/@/hooks/web/usePage';

  export default defineComponent({
    name: 'AccountManagement',
    components: {
      BasicTable,
      PageWrapper,
      DeptTree,
      MngModal,
      AccountModal,
      AccountPwdModal,
      TableAction,
    },
    setup() {
      const go = useGo();
      const [registerModal, { openModal }] = useModal();
      const [registerModal2, { openModal: openModal2 }] = useModal();
      const [registerModal3, { openModal: openModal3 }] = useModal();
      const searchInfo = reactive<Recordable>({});
      const [registerTable, { reload, updateTableDataRecord }] = useTable({
        title: '调查单位列表',
        api: getSurveyUnitList,
        rowKey: 'id',
        columns,
        formConfig: {
          schemas: searchFormSchema,
          autoSubmitOnEnter: true,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        handleSearchInfoFn(info) {
          console.log('handleSearchInfoFn', info);
          return info;
        },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });

      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function getDistrictOne(value) {
        if (value) {
          return value.regionName;
        }
        return value;
      }

      function getDistrictMany(value) {
        if (value) {
          const fds: string[] = [];
          value.forEach((element) => {
            fds.push(element.regionName);
          });

          if (fds.length > 0) {
            return fds.toString();
          }
        }

        return value;
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      function handleEditMng(record: Recordable) {
        openModal3(true, {
          record,
          isUpdate: true,
        });
      }

      function handleEditPwd(record: Recordable) {
        openModal2(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await delSurveyUnit(record);
        reload();
      }

      function handleSuccess({ isUpdate, values }) {
        if (isUpdate) {
          // 演示不刷新表格直接更新内部数据。
          // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
          const result = updateTableDataRecord(values.id, values);
          console.log(result);
        } else {
          reload();
        }
      }

      function handleSelect(deptId = '') {
        searchInfo.departmentId = deptId;
        reload();
      }

      function handleView(record: Recordable) {
        go('/system/account_detail/' + record.id);
      }

      return {
        registerTable,
        registerModal,
        registerModal2,
        registerModal3,
        handleCreate,
        handleEdit,
        handleEditMng,
        handleEditPwd,
        handleDelete,
        handleSuccess,
        handleSelect,
        handleView,
        searchInfo,
        getDistrictOne,
        getDistrictMany,
      };
    },
  });
</script>
